package com.DongBing.service.impl;

import com.DongBing.common.utils.Res;
import lombok.val;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.DongBing.common.utils.PageUtils;
import com.DongBing.common.utils.Query;

import com.DongBing.dao.CategoryDao;
import com.DongBing.entity.CategoryEntity;
import com.DongBing.service.CategoryService;

@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 返回公交站点的全部信息
     * @return 返回树形菜单
     */
    @Override
    public Res<List<CategoryEntity>> listTree() {
        val list = baseMapper.selectList(null);
        val list_plus = list.stream()
                .filter(e -> e.getParentCid().equals(0L))
                .peek(e->{
                    val list_2 = new ArrayList<CategoryEntity>();
                    list.forEach(x->{
                        if (e.getCatId().equals(x.getParentCid())) {
                            val list_3 = new ArrayList<CategoryEntity>();
                            list.forEach(s->{
                                if (x.getCatId().equals(s.getParentCid())) {
                                    list_3.add(s);
                                }
                            });
                            x.setList(list_3);
                            list_2.add(x);
                        }
                    });
                    e.setList(list_2);
                })
                .collect(Collectors.toList());
        return Res.success("查询成功",list_plus);
    }

    @Override
    public Res deleteIds(Long[] catIds) {
        baseMapper.deleteBatchIds(Arrays.asList(catIds));
        return Res.success("删除成功");
    }

    @Override
    public Res<?> delete(String ids) {
        val num = baseMapper.deleteById(ids);
        if (num==1) {
            return Res.success("删除成功");
        }
        return Res.error("删除失败");
    }

    @Override
    public Res<?> insert(CategoryEntity categoryEntity) {
        val insert = baseMapper.insert(categoryEntity);
        if (insert==1) {
            return Res.success("插入成功");
        }
        return Res.error("插入失败");
    }

    @Override
    public Res<?> update(CategoryEntity categoryEntity) {
        val update = baseMapper.updateById(categoryEntity);
        if (update==1) {
            return Res.success("修改成功");
        }
        return Res.error("修改失败");
    }

    @Override
    public Res<?> deletes(Long[] ids) {
        val batchIds = baseMapper.deleteBatchIds(Arrays.asList(ids));
        if (batchIds>0) {
            return Res.success("批量删除成功");
        }
        return Res.error("删除失败");
    }

    @Override
    public Long[] findPath(Long attrGroupId) {
        val list = new ArrayList<Long>();
        val path = this.findPath(attrGroupId,list);
        Collections.reverse(path);
        // 指定返回数组的类型 否则Object[] 报错
        return path.toArray(new Long[list.size()]);
    }

    public List<Long> findPath(Long attrGroupId,List<Long> list){
        list.add(attrGroupId);
        val entity = baseMapper.selectById(attrGroupId);
        if (0!=entity.getParentCid()) {
            this.findPath(entity.getParentCid(),list);
        }
        return list;
    }

}
